from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from app.core.config import settings
from app.db.database import engine
from app.models import Base

# Create database tables
Base.metadata.create_all(bind=engine)

# Create FastAPI app
app = FastAPI(
    title="Shop Management API",
    description="商品管理系统API",
    version="1.0.0"
)

# Add CORS middleware
app.add_middleware(
    CORSMiddleware,
    allow_origins=[
        "http://localhost:3000",  # 本地开发
        "http://101.33.122.140:3000",  # 公网地址
        "http://101.33.122.140",  # 不带端口的公网地址
        "https://101.33.122.140:3000",  # HTTPS
        "https://101.33.122.140",  # HTTPS不带端口
        "*"  # 允许所有来源（开发环境）
    ],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Include routers
from app.api import auth, categories, products, homepage, upload
app.include_router(auth.router, prefix="/api/v1/auth", tags=["auth"])
app.include_router(categories.router, prefix="/api/v1/categories", tags=["categories"])
app.include_router(products.router, prefix="/api/v1/products", tags=["products"])
app.include_router(homepage.router, prefix="/api/v1/homepage", tags=["homepage"])
app.include_router(upload.router, prefix="/api/v1/upload", tags=["upload"])

@app.get("/")
async def root():
    return {"message": "Shop Management API is running!"}

@app.get("/health")
async def health_check():
    return {"status": "healthy"} 